package com.scsystem.xysc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.scsystem.xysc.pojo.Comment;
import com.scsystem.xysc.pojo.vo.ReturnResult;
import com.scsystem.xysc.service.CommentService;
import com.scsystem.xysc.mapper.CommentMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author sky
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2023-09-16 15:56:45
*/
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment>
    implements CommentService{


    @Resource
    private CommentMapper commentMapper;


    @Override
    public ReturnResult<Boolean> addComment(Long uid, Long pid, String content, Integer rating) {
        // 创建一个新的评论对象
        Comment comment = new Comment();
        // 设置用户ID
        comment.setUserId(uid);
        // 设置产品ID
        comment.setProductId(pid);
        // 设置评论内容
        comment.setContent(content);
        // 设置评分
        comment.setRating(rating);
        // 设置用户喜欢数量
        comment.setUserLike(0);
        // 设置发布日期为当前时间
        comment.setPublishTime(LocalDateTime.now());
        // 将评论插入到评论数据表中
        int insert = commentMapper.insert(comment);
        // 如果插入成功，返回发表成功的信息和true；否则返回发表失败的信息和false
        return insert == 1 ? new ReturnResult<>(200, "发表成功", true) : new ReturnResult<>(400, "发表失败", false);
    }

}




